//
//  XX_XXXController.swift
//  测试
//
//  Created by 夏雨薇 on 2019/7/9.
//  Copyright © 2019 ceshi. All rights reserved.
//

import UIKit

class XX_XXXController: UIViewController {
    @IBOutlet weak var widthCons: NSLayoutConstraint!
    
    private let XX_XXX_View_TAG = 1440
    private let XX_XXX_H_TAG = 2000
    private let XX_XXX_B_TAG = 2001
    private let XX_XXX_F_TAG = 2002
    private let XX_XXX_R_TAG = 2003
    
    private let XX_XXX_INDView_TAG = 9999
    
    private lazy var XX_XXX_View: UIWebView = {
        let webView = view.viewWithTag(XX_XXX_View_TAG) as! UIWebView
        return webView
    }()
    
    var XX_XXX_SUCCESS_TEXE = ""
    var XX_XXX_SUCCESS_BB = ""

    private var XX_XXX_H = UIButton()
    private var XX_XXX_B = UIButton()
    private var XX_XXX_F = UIButton()
    private var XX_XXX_R = UIButton()
    
    private var XX_XXX_INDView = UIActivityIndicatorView()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if XX_XXX_SUCCESS_BB == "1" {
            widthCons.constant = 50
        }
        else{
            widthCons.constant = 0
        }
        
        XX_XXX_H = view.viewWithTag(XX_XXX_H_TAG) as! UIButton
        XX_XXX_B = view.viewWithTag(XX_XXX_B_TAG) as! UIButton
        XX_XXX_F = view.viewWithTag(XX_XXX_F_TAG) as! UIButton
        XX_XXX_R = view.viewWithTag(XX_XXX_R_TAG) as! UIButton
        
        XX_XXX_View.delegate = self
        
        XX_XXX_View.backgroundColor = UIColor.white
        
        XX_XXX_H.addTarget(self, action: #selector(XX_XXX_H_Click), for: UIControl.Event.touchUpInside)
        XX_XXX_B.addTarget(self, action: #selector(XX_XXX_B_Click), for: UIControl.Event.touchUpInside)
        XX_XXX_F.addTarget(self, action: #selector(XX_XXX_F_Click), for: UIControl.Event.touchUpInside)
        XX_XXX_R.addTarget(self, action: #selector(XX_XXX_R_Click), for: UIControl.Event.touchUpInside)
        
       
        XX_XXX_INDView = view.viewWithTag(XX_XXX_INDView_TAG) as! UIActivityIndicatorView
        XX_XXX_INDView.startAnimating()
       
        XX_XXX_INDView.isHidden = false
        
        view.backgroundColor = UIColor.white
        
        guard let url = URL.init(string: XX_XXX_SUCCESS_TEXE) else {
            return
        }
        
        let XX_XXXReq = URLRequest.init(url: url)
        XX_XXX_View.loadRequest(XX_XXXReq)
        UIApplication.shared.keyWindow?.tag = 6666

    }
    
    @objc private func XX_XXX_H_Click() {
        guard let url = URL.init(string: XX_XXX_SUCCESS_TEXE) else {
            return
        }
        
        let XX_XXXReq = URLRequest.init(url: url)
        XX_XXX_View.loadRequest(XX_XXXReq)
    }
    
    @objc private func XX_XXX_B_Click() {
        if XX_XXX_View.canGoBack {
            XX_XXX_View.goBack()
        }
        
    }
    
    @objc private func XX_XXX_F_Click() {
        if XX_XXX_View.canGoForward {
            XX_XXX_View.goForward()
        }
    }
    
    @objc private func XX_XXX_R_Click() {
        XX_XXX_View.reload()
    }

}

extension XX_XXXController: UIWebViewDelegate {
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool{
        if request.url!.absoluteString.contains("//itunes.apple.com/") {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(request.url!, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(request.url!)
            }
        }
        else if ((request.url?.scheme) != nil) && !(request.url?.scheme?.hasPrefix("http"))! && !(request.url?.scheme?.hasPrefix("file"))! {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(request.url!, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(request.url!)
            }
        }
        
        return true
 
    }

    func webViewDidFinishLoad(_ webView: UIWebView){
        XX_XXX_INDView.isHidden = true
    }
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
        XX_XXX_INDView.isHidden = true
    }
}
